﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Data;
using Aden.Util.Database;
using Aden.Util.Common;
using Aden.Model.Stock;

namespace Aden.DAL.StockData
{
    public class StockTransFactory
    {
        private StockTransfer stockTransfer = null;

        public StockTransFactory(string appGuid, string costCenterCode, string orderDate, string createUser)
        {
            try
            {
                string sql = "select classname from tblapps where guid='{0}'";
                sql = string.Format(sql, appGuid);
                string className =  SqlServerHelper.GetDataScalar(SqlServerHelper.salesorderConn(), sql).ToStringTrim();
                
                if (string.IsNullOrWhiteSpace(className))
                    throw new Exception("No set class for " + appGuid);

                object[] objs = {appGuid, costCenterCode, orderDate, createUser };

                Type type = Type.GetType("Aden.DAL.StockData." + className);
                stockTransfer = (StockTransfer)Activator.CreateInstance(type, objs);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public List<Dictionary<string,object>> GetItems(StockParam param)
        {
            try
            {
                //Dictionary<string, string> dicparam = null;
                //if (!string.IsNullOrWhiteSpace(param.parameters))
                //    dicparam = JsonHelper.JSDeserialize<Dictionary<string, string>>(param.parameters);

                //return stockTransfer.GetItems(dicparam);
                return stockTransfer.GetItems(param);
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public List<Dictionary<string, object>> GetDates()
        {
            try
            {
                return stockTransfer.GetDates();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    
        public Dictionary<string,object> save(StockParam param)
        {
            try
            {
                //Dictionary<string, string> dicparam = null;
                //if (!string.IsNullOrWhiteSpace(param.parameters))
                //    dicparam = JsonHelper.JSDeserialize<Dictionary<string, string>>(param.parameters);

                //return stockTransfer.save(dicparam,param.data);
                return stockTransfer.save(param);
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public Dictionary<string, object> ShowComponents()
        {
            try
            {
                return stockTransfer.ShowComponents();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
